package com.wanpinghui.supplier.user.order;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.user.order.adapter.OrderGoodsAdapter;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.order.OrderService;
import com.wanpinghui.wphlibrary.model.order.bean.OrderItemBean;
import com.wanpinghui.wphlibrary.model.order.bean.OrderRemarkBean;
import com.yunshl.yunshllibrary.utils.TextUtil;
import com.yunshl.yunshllibrary.utils.TimeUtil;
import com.yunshl.yunshllibrary.view.YunShlEditText;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import retrofit.callback.YRequestCallback;

/**
 * 功能说明：
 * Created by caitiangui
 * 创建时间： 2017/12/22.
 */
@ContentView(R.layout.layout_activity_order_detail)
public class OrderDetailActivity extends BaseActivity {

    @ViewInject(R.id.tpl_title)
    private TitlePanelLayoutUser tpl_title;

    @ViewInject(R.id.recyclerview)
    private RecyclerView super_recycle_view;

    @ViewInject(R.id.ll_order_status)
    private LinearLayout ll_order_status;

    @ViewInject(R.id.edt_input)
    private YunShlEditText edt_input;

    @ViewInject(R.id.tv_name)
    private TextView tv_name;

    @ViewInject(R.id.scrollView)
    private ScrollView scrollView;

//    @ViewInject(R.id.tv_order_code_time)
//    private TextView textViewCodeTime;

    @ViewInject(R.id.tv_order_code)
    private TextView tv_order_code;

    @ViewInject(R.id.tv_order_time)
    private TextView tv_order_time;

//    @ViewInject(R.id.btn_submit)
//    private ThrottleButton btn_submit;

    private OrderGoodsAdapter adapter;

    private OrderItemBean mOrderItem;
    private int orderId;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {

    }

    @Override
    public void bindEvents() {
        tpl_title.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.tv_payments).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt_input.setText("已收款");
            }
        });
        findViewById(R.id.tv_delivery).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt_input.setText("已发货");
            }
        });
        findViewById(R.id.tv_qianshou).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt_input.setText("已签收");
            }
        });
        findViewById(R.id.btn_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (TextUtil.isNotEmpty(edt_input.getTextString()) && mOrderItem != null) {
                    OrderRemarkBean orderRemarkBean = new OrderRemarkBean(mOrderItem.getId(), edt_input.getTextString());
                    WPHSdk.getService(OrderService.class).addOrderRemark(orderRemarkBean, new YRequestCallback<List<OrderRemarkBean>>() {
                        @Override
                        public void onSuccess(List<OrderRemarkBean> var1) {
//                            bean.getRemarkList().add(0,var1);
                         //   showWarningText("订单状态已更新");
                            edt_input.setText("");
                            if (var1 != null && var1.size() > 0) {
                                ll_order_status.removeAllViews();
                                for (int i = 0; i < var1.size(); i++) {
                                    ll_order_status.addView(addOrderStatus(var1.get(i)));
                                    scrollView.fullScroll(ScrollView.FOCUS_UP);
                                }
                            }
                        }

                        @Override
                        public void onFailed(int var1, String message) {
                            showWarningText(message);
                        }

                        @Override
                        public void onException(Throwable var1) {
                            var1.printStackTrace();
                        }
                    });
                    //   LogUtils.w(str_json);
                } else {
                    showWarningText("您还没输入内容");
                }
            }
        });
    }

    @Override
    public void initData() {
        String orderStr = getIntent().getStringExtra("order");
        if (TextUtil.isNotEmpty(orderStr)) {
            mOrderItem = new Gson().fromJson(orderStr, new TypeToken<OrderItemBean>() {
            }.getType());
            setData(mOrderItem);
            if (mOrderItem.getId() != 0) {
                WPHSdk.getService(OrderService.class).getOrderDetail(mOrderItem.getId(), new YRequestCallback<OrderItemBean>() {
                    @Override
                    public void onSuccess(OrderItemBean var1) {
                        setData(var1);
                    }

                    @Override
                    public void onFailed(int var1, String message) {
                        showWarningText(message);
                    }

                    @Override
                    public void onException(Throwable var1) {

                    }
                });
            }
        }

    }

    private void setData(OrderItemBean orderItemBean) {
        if (orderItemBean != null) {
            adapter = new OrderGoodsAdapter(this);
            super_recycle_view.setNestedScrollingEnabled(false);
            super_recycle_view.setLayoutManager(new LinearLayoutManager(this));
            super_recycle_view.setAdapter(adapter);
            adapter.setDatas(orderItemBean.getItemList());
            tv_name.setText("采购人： " + orderItemBean.getBuyer_name() + "   " + orderItemBean.getSafeBuyerMobile());
            tv_order_code.setText("订单编号：" + orderItemBean.getOrder_no());
            tv_order_time.setText(TimeUtil.format(orderItemBean.getCreated_at() * 1000, "yyyy-MM-dd HH:mm"));
            //    textViewCodeTime.setText(+"    下单时间："+ );
            if (orderItemBean.getRemarkList() != null && orderItemBean.getRemarkList().size() > 0) {
                ll_order_status.removeAllViews();
                for (int i = 0; i < orderItemBean.getRemarkList().size(); i++) {
                    ll_order_status.addView(addOrderStatus(orderItemBean.getRemarkList().get(i)));
                }
            }

        }
    }

    private View addOrderStatus(OrderRemarkBean bean) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_order_status, null);
        TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
        TextView tv_time = (TextView) view.findViewById(R.id.tv_time);
        tv_content.setText(bean.getRemark());
        tv_time.setText(TimeUtil.format(bean.getCreated_at() * 1000, "yyyy-MM-dd HH:mm:ss"));
        return view;
    }


}
